package com.example.cxx.Utils;

import com.example.cxx.controller.LoginController;
import com.example.cxx.controller.TestController;
import com.example.cxx.mapper.ScoreSubMapper;
import com.example.cxx.mapper.UserMapper;
import com.example.cxx.pojo.Result;
import com.example.cxx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;

@Component
public class SchedularTask {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ScoreSubMapper scoreSubMapper;
    @Autowired
    private LoginController loginController;
    @Autowired
    private TestController testController;

//    @Scheduled( cron ="*/5 * * * * ?")
    public void scoreTask() throws IOException {
      Result res;
      List<String> openidList=scoreSubMapper.selectAllSubOpenid();
        for (String s : openidList) {
            Integer userId= scoreSubMapper.selectUserIdByOpenId(s);
            User user=userMapper.selectUserById(userId);
            do{
                res=loginController.loginWithNoCode(String.valueOf(user.getUserAccount()),user.getUserPass());
            }
            while (res.getCode()==0);
            testController.testCookie2(res.getData().toString());
            //这里是定时任务获取的成绩数量
            Integer taskNowScoreNum=testController.scoreNum;
            //1.先判断update表是否有数据 ---如果有进行比较一次数目 有变化则发送通知 然后修改数据库数据 ----如果没有则进行一次插入数据
            if(scoreSubMapper.selectScoreUpdateByUserAccount(user.getUserAccount())){
                Integer lastScoreNum=scoreSubMapper.selectScoreNumByUserAccount(user.getUserAccount());
                if(taskNowScoreNum>lastScoreNum){

                }
                scoreSubMapper.updateScoreUpdate(user.getUserAccount(),taskNowScoreNum);
            }
            else {
                scoreSubMapper.insertScoreUpdate(user.getUserAccount(),taskNowScoreNum);
            }

            //先发送后update
        }
    }
}
